package com.example.yuecvideo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.yuecvideo.R;
import com.example.yuecvideo.dao.DBDao;
import com.example.yuecvideo.utils.ConnectUtil;
import com.example.yuecvideo.utils.RandomUtil;
import com.example.yuecvideo.utils.ToastUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RegisterActivity extends AppCompatActivity {
    private EditText account, password, surePasswrod;
    private Button register_btn;
    private String account_str, password_str, userName_str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activate_register);
        init();
        listenInit();
    }

    private void init() {
        account = (EditText) findViewById(R.id.user_reg);
        password = (EditText) findViewById(R.id.pass_reg);
        surePasswrod = (EditText) findViewById(R.id.sPass_reg);
        register_btn = (Button) findViewById(R.id.regist_reg);
    }

    private void listenInit() {
        register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkStr()) {
                    return;
                }
                checkDB();
            }
        });
    }

    /**
     * @brief 确定各字符串是否满足
     */
    private boolean checkStr() {
        Context contex = getApplicationContext();
        account_str = account.getText().toString();
        password_str = password.getText().toString();
        String sPass = surePasswrod.getText().toString();
        if (ToastUtil.Empty(account_str, "账户", contex)) {
            return false;
        }
        if (ToastUtil.Empty(password_str, "密码", contex)) {
            return false;
        }
        if (ToastUtil.Empty(sPass, "确认密码", contex)) {
            password.clearComposingText();
            return false;
        }
        if (ToastUtil.NotSync(password_str, sPass, "密码", contex)) {
            password.clearComposingText();
            surePasswrod.clearComposingText();
            password.setFocusable(true);
            return false;
        }
        return true;
    }

    /**
     * @brief 确保该用于尚未注册过
     * @detalis 检测数据库中是否存在
     * 若存在则将输入框清空
     * 若不存在则插入数据，然后再跳转到登录页面
     */
    private void checkDB() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String sql = "select id from user where account = " + account_str;
                try {
                    ResultSet resultSet = DBDao.Select(sql);
                    Context contex = getApplicationContext();
                    if (resultSet.next()) {

                        return;
                    }
                    sql = "insert into user(account, userName, password) values(" +
                            account_str + "," + RandomUtil.RandomPlusTime(account_str) +
                            "," + password_str + ")";
                    if (!DBDao.Update(sql)) {
                        jumpLoginActivaty();
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }
        }).start();

    }

    private void jumpLoginActivaty() {
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("account", account_str);
        bundle.putString("password", password_str);
        intent.putExtra("bundle", bundle);
        startActivity(intent);
    }
}
